﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WS.FlowAuth.Lib.Test
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            for (var i = 0; i < 10; ++i)
            {
                _edt_flows.Add(Controls.Find($"edt_flow{i + 1}", true)[0] as TextBox);
                _edt_errs.Add(Controls.Find($"edt_err{i + 1}", true)[0] as TextBox);
            }
        }

        private void cbo_point_SelectedIndexChanged(object sender, EventArgs e)
        {
            var n = cbo_point.SelectedIndex;
            for (var i = 0; i < 10; ++i)
            {
                _edt_flows[i].Enabled = i <= n;
                _edt_errs[i].Enabled = i <= n;
            }
            if (n == 0)
            {
                _edt_flows[0].Enabled = false;
                _edt_errs[0].Enabled = false;
            }
        }

        private void btn_read_Click(object sender, EventArgs e)
        {
            for (var i = 0; i < 10; ++i)
            {
                _edt_flows[i].Text = i.ToString();
                _edt_errs[i].Text = (i / 100.0).ToString("F2");
            }
            cbo_point.SelectedIndex = 9;
        }

        private void add_float(byte[] dest, int offset, float value)
        {
            var buf = BitConverter.GetBytes(value);
            dest[offset] = buf[0];
            dest[offset + 1] = buf[1];
            dest[offset + 2] = buf[2];
            dest[offset + 3] = buf[3];
        }

        private void btn_write_Click(object sender, EventArgs e)
        {
            var n = cbo_point.SelectedIndex;
            if (n < 0)
            {
                MessageBox.Show(@"请选择修正点数");
                return;
            }
            byte[] opt_data;
            if (n == 0)
            {
                opt_data = new byte[] { 0x00, 0x00 };
            }
            else
            {
                opt_data = new byte[2 + (n + 1) * 8];

                var countBytes = BitConverter.GetBytes(n + 1);

                opt_data[0] = countBytes[0];// 0;
                opt_data[1] = countBytes[1];// (byte)(n + 1);
                for (var i = 0; i < n + 1; ++i)
                {
                    add_float(opt_data, 2 + i * 8, float.Parse(_edt_flows[i].Text));
                    add_float(opt_data, 2 + i * 8 + 4, float.Parse(_edt_errs[i].Text));
                }
            }
            var args = new GetQrCodeArgs()
            {
                flowNo = "123456",
                optData = opt_data,
                optCode = "A1"
            };
            using (var frm = new FrmAuth(args))
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show(@"写入成功");
                }
                else
                {
                    MessageBox.Show(@"写入失败");
                }
            }
        }

        private readonly List<TextBox> _edt_flows = new List<TextBox>();
        private readonly List<TextBox> _edt_errs = new List<TextBox>();
    }
}
